package cn.admin.configcenter.registry.service.client;

import cn.admin.config.common.Revision;
import cn.admin.config.common.protocol.Swizzle;
import cn.admin.configcenter.registry.common.info.ClientConnection;
import cn.admin.configcenter.registry.common.info.GroupId;
import cn.admin.configcenter.registry.service.DataService;
import cn.admin.configcenter.registry.service.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DefaultDataService implements DataService {

	private static final Logger DATAMONITOR = LoggerFactory.getLogger("DATAMONITOR");
	private final DataSource nonPersistDS;

	public DefaultDataService(NonPersistDataMerge dataMerge) {
		this.nonPersistDS = new NonPersistDataSource(dataMerge);
	}

	@Override
	public void addPublisher(ClientConnection client, GroupId group, String clientId, String datumId) {
		group = matchingGroup(client,"publisher",group);
		nonPersistDS.addPublisher(client,group,clientId,datumId);
	}

	@Override
	public void publishData(ClientConnection client, String dataId,
	                        String clientId, List<Swizzle> data, Revision revision) {
		GroupId group = client.getGroupIdByClientId(dataId,clientId);
		if (group != null) {
			nonPersistDS.publishData(client,group,clientId,data,revision);
			DATAMONITOR.info("[data-submit-master] {}, {}, revision={}", client, group, revision);
		} else {
			DATAMONITOR.info("[data-submit-master] publisher not found. {}, {}, revision={}",
					client, dataId, revision);
		}
	}

	@Override
	public void publishSyncData(ClientConnection client, String dataId, String clientId, List<Swizzle> data, Revision revision, String originalCsIp) {
		GroupId group = client.getGroupIdByClientId(dataId,clientId);
		if (group != null) {
			nonPersistDS.publishData(client,group,clientId,data,revision);
			DATAMONITOR.info("[data-submit-slave] {}, {}, revision={} from {}",
					client, group, revision, originalCsIp);
		} else {
			DATAMONITOR.info("[data-submit-slave] publisher not found. {}, {}, revision={} from ",
					client, dataId, revision, originalCsIp);
		}
	}

	@Override
	public void addSubscriber(ClientConnection client, GroupId group, String clientId) {
		group = matchingGroup(client,"subscriber",group);
		nonPersistDS.addSubscriber(client,group,clientId);
	}

	@Override
	public List<Swizzle> getData(GroupId group) throws Exception {
		Map<String,Swizzle> datum2data = this.getDatumData(group);
		return new ArrayList<>(datum2data.values());
	}

	@Override
	public List<Swizzle> getPushData(GroupId group) throws Exception {
		return this.nonPersistDS.getPushData(group);
	}

	@Override
	public Map<String, Swizzle> getDatumData(GroupId group) throws Exception {
		return this.nonPersistDS.getDatumData(group);
	}

	@Override
	public List<GroupId> allDataGroups() throws Exception {
		return this.nonPersistDS.allDataGroups();
	}

	private GroupId matchingGroup(ClientConnection client,String type,GroupId requestGroup) {
		return GroupId.getSingleton(requestGroup.dataId,requestGroup.groupId);
	}
}
